<template>
  <el-dialog v-model="dialogVisible" width="540" title="请输入企业名称" destroy-on-close :show-close="false"
    :close-on-click-modal="false" custom-class="login-form-dialog">
    <el-form ref="formRef" v-loading="formLoading" :model="form" :rules="rules" label-position="top"
      label-width="120px">
      <el-form-item prop="cargoOwnerName">
        <el-input v-model="form.cargoOwnerName" :maxlength="100" placeholder="请输入企业名称">
          <template #prefix>
            <img src="@/assets/imgs/enterprise-icon.png" />
          </template>
        </el-input>
      </el-form-item>
      <div class="tip-box">
        <img src="@/assets/imgs/tip-icon.png" mode="scaleToFill" style="width: 20px;height: 20px;" />
        请输入自己的企业名称
      </div>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button type="primary" @click="submitForm"> 注册并登录 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import * as LoginApi from '@/api/login'
  import * as register from '@/api/register'
  import { cloneDeep } from 'lodash'
  import * as authUtil from '@/utils/auth'

  const emit = defineEmits(['success'])
  const message = useMessage()
  const validMess = ref('获取验证码')
  const formLoading = ref(false)
  const dialogVisible = ref(false)
  const formRef = ref()
  const form = ref({
    mobile: undefined,
    cargoOwnerName: undefined
  })

  const rules = reactive({
    cargoOwnerName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
  })

  const open = async (mobile) => {
    resetFn()
    form.value.mobile = mobile
    dialogVisible.value = true
  }

  const submitForm = async () => {
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    try {
      formLoading.value = true
      const res = await LoginApi.registerAndLogin(form.value)
      authUtil.setToken(res)
      authUtil.setUserId(res.userId)
      authUtil.setTenantId(res.tenantId)
      message.success('注册并登录成功')
      emit('success')
      dialogVisible.value = false
    } finally {
      formLoading.value = false
    }
  }

  const resetFn = () => {
    form.value = {
      mobile: undefined,
      cargoOwnerName: undefined
    }
    formRef.value?.resetFields()
  }

  defineExpose({ open })
</script>

<style lang="scss" scoped>
  .el-button.code {
    color: #409EFE !important;
  }

  .tip-box {
    display: flex;
    padding: 8px 10px;
    color: #e6a23c;
    border-radius: 4px;
    font-size: 14px;
    background: rgba(230, 162, 60, 0.10);

    img {
      margin-right: 2px;
    }
  }

  :deep(.el-input--default) {
    height: 40px;
    line-height: 40px;
    font-size: 16px;
  }

  :deep(.el-button--large) {
    font-size: 16px;
  }

  :deep(.el-input__password) {
    font-size: 18px;
  }
</style>

<style lang="scss">
  .el-dialog.login-form-dialog {
    background: linear-gradient(181deg, #cae4ff 0%, #ffffff 24%);
    box-shadow: 0px 12px 48px 0px rgba(0, 0, 0, 0.03), 0px 9px 28px 0px rgba(0, 0, 0, 0.05), 0px 6px 16px 0px rgba(0, 0, 0, 0.08);
  }

  .login-form-dialog .el-dialog__header {
    padding: 30px 40px;
  }

  .login-form-dialog .el-dialog__title {
    font-weight: 500;
    font-size: 28px;
    font-family: PingFang SC, PingFang SC-500;
  }

  .login-form-dialog .el-dialog__body {
    padding: 0px 40px !important;
  }

  .login-form-dialog .el-dialog__footer {
    margin-top: 50px;
    padding: 10px 20px;
    border-top: 1px solid #E4E7ED;
  }
</style>